<?php

namespace App\Repository;

use App\Common\Libs\ArrayHelper;
use App\Models\Config;

class ConfigRepository extends BaseRepository
{
    /**
     * 通过键获取配置
     * @param $key
     * @return mixed
     */
    public function getConfigByKey($key)
    {
        return Config::getInstance()
            ->where('key', $key)
            ->value('value');
    }

    /**
     * 通过键获取配置
     * @param $keys
     * @return array
     */
    public function getConfigsByKeys($keys)
    {
        $result = [];

        $configs = Config::getInstance()
            ->whereIn('key', (array)$keys)
            ->get();
        if (empty($configs->toArray())) {
            foreach ($keys as $key) {
                $result[$key] = '';
            }

            return $result;
        }

        $configs = ArrayHelper::index($configs, 'key');
        foreach ($keys as $key) {
            $result[$key] = $configs[$key]->value ?? '';
        }

        return $result;
    }
}
